package com.beta.cust.bind.webapi.controller;

import com.beta.cat.utils.JsonUtil;
import com.beta.cat.utils.ReflectUtils;
import com.beta.cust.bind.api.request.CustAuthReq;
import com.beta.cust.bind.api.request.CustDetailReq;
import com.beta.cust.bind.api.request.CustListReq;
import com.beta.cust.bind.api.response.CustDetailResp;
import com.beta.cust.bind.api.response.CustListResp;
import com.beta.cust.bind.api.service.IRpcCustService;
import com.beta.cust.bind.webapi.config.CfgSelfRefreshBean;
import com.beta.cust.bind.webapi.constant.ResultCode;
import com.beta.cust.bind.webapi.request.CustAuthRequest;
import com.beta.cust.bind.webapi.request.CustDetailRequest;
import com.beta.cust.bind.webapi.request.CustListReqest;
import com.beta.cust.bind.webapi.response.CustDetailResponse;
import com.beta.cust.bind.webapi.response.CustListResponse;
import com.beta.cust.bind.webapi.utils.CommonUtil;
import com.beta.elephant.vo.ResultVO;
import com.beta.platform.rest.auth.LoginAccountDetail;
import com.beta.platform.rest.auth.LoginAccountSecurity;
import com.beta.platform.rest.auth.Signet;
import com.beta.platform.rest.auth.SignetSecurity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: pcc
 * @Date: 2024/3/27 17:41
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/cust")
public class CustController {

    @DubboReference
    private IRpcCustService rpcCustService;

    @Autowired
    private CfgSelfRefreshBean cfgSelfRefreshBean;

    @GetMapping("/detail")
    public ResultVO<CustDetailResponse> detail(CustDetailRequest detailReq) {
        LoginAccountDetail loginAccountDetail = LoginAccountSecurity.getLoginAccountDetail();
        if (ObjectUtils.anyNull(detailReq.getContactUserId())) {
            return ResultVO.failure(ResultCode.ERROR_CODE_1101);
        }
        CustDetailReq req = new CustDetailReq();
        req.setContactUserId(detailReq.getContactUserId());
        req.setUserId(loginAccountDetail.getThirdUserInfo().getUserId());
        req.setCorpId(loginAccountDetail.getThirdUserInfo().getCorpId());
        log.info("detail param:{}", JsonUtil.toJson(detailReq));
        ResultVO<CustDetailResp> detailResp = rpcCustService.custDetail(req);
        if (detailResp.getCode() != ResultCode.SUCCESS.getCode()) {
            return ResultVO.failure(detailResp.getCode(), detailResp.getMsg());
        }
        return ResultVO.success(ReflectUtils.convertEntity(detailResp.getData(), CustDetailResponse.class));
    }

    @GetMapping("/list")
    public ResultVO<CustListResponse> list(CustListReqest listReq) {
        CustListReq req = new CustListReq();
        req.setCertified(listReq.getCertified());
        LoginAccountDetail loginAccountDetail = LoginAccountSecurity.getLoginAccountDetail();
        req.setUserId(loginAccountDetail.getThirdUserInfo().getUserId());
        req.setCorpId(loginAccountDetail.getThirdUserInfo().getCorpId());
        req.setSearchParam(listReq.getSearchParam());
        req.setPageNo(listReq.getPageNo());
        req.setPageSize(listReq.getPageSize());
        log.info("list param:{}", JsonUtil.toJson(req));
        ResultVO<CustListResp> listResp = rpcCustService.custList(req);
        if (listResp.getCode() != ResultCode.SUCCESS.getCode()) {
            return ResultVO.failure(listResp.getCode(), listResp.getMsg());
        }
        return ResultVO.success(ReflectUtils.convertEntity(listResp.getData(), CustListResponse.class));
    }

    @GetMapping("/latest/auth/phone")
    public ResultVO<String> latestAuthPhone(CustDetailRequest latestReq) {
        CustDetailReq req = new CustDetailReq();
        LoginAccountDetail loginAccountDetail = LoginAccountSecurity.getLoginAccountDetail();
        req.setUserId(loginAccountDetail.getThirdUserInfo().getUserId());
        req.setCorpId(loginAccountDetail.getThirdUserInfo().getCorpId());
        log.info("latestAuthPhone param:{}", JsonUtil.toJson(req));
        ResultVO<String> resp = rpcCustService.latestAuthPhone(req);
        if (resp.getCode() != ResultCode.SUCCESS.getCode()) {
            return ResultVO.failure(resp.getCode(), resp.getMsg());
        }
        return ResultVO.success(CommonUtil.maskPhone(resp.getData()));
    }

    @PostMapping("/auth")
    public ResultVO<Boolean> auth(@RequestBody CustAuthRequest authReq) {
        log.info("auth param:{}", JsonUtil.toJson(authReq));
        LoginAccountDetail loginAccountDetail = LoginAccountSecurity.getLoginAccountDetail();
        authReq.setUserId(loginAccountDetail.getThirdUserInfo().getUserId());
        if (ObjectUtils.anyNull(authReq.getUserId(), authReq.getCustPhone())) {
            return ResultVO.failure(ResultCode.ERROR_CODE_1101);
        }
        log.info("auth param:{}", JsonUtil.toJson(authReq));
        CustAuthReq req = ReflectUtils.convertEntity(authReq, CustAuthReq.class);
        req.setCorpId(loginAccountDetail.getThirdUserInfo().getCorpId());
        ResultVO resp = rpcCustService.custAuth(req);
        if (resp.getCode() != ResultCode.SUCCESS.getCode()) {
            return ResultVO.failure(resp.getCode(), resp.getMsg());
        }
        return resp;
    }
}
